package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/settings/qx/user/toLogin.do")
    public String toLogin(){
        return "settings/qx/user/login";
    }

    @RequestMapping(value = "/settings/qx/user/login.do")
    @ResponseBody
    public Object login(String loginAct, String loginPwd, String isRemPwd, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        /*Cookie[] cookies = request.getCookies();
        for(Cookie cookie:cookies){
            if("loginAct".equals(cookie.getName())){
                loginAct = cookie.getValue();
            }else if("loginPwd".equals(cookie.getName())){
                loginPwd = cookie.getValue();
            }
        }*/
        Map<String,Object> map = new HashMap<>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);
        //调用service
        User user = userService.queryUserByLoginActAndPwd(map);

        ReturnObject returnObject = new ReturnObject();

        //根据查询结果判断响应信息
        if(user == null){
            //登入失败,用户名或密码错误
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("用户名或密码失败");
        }else{
            //用户名密码正确
            String nowStr = DateUtils.formatDataTime(new Date());
            if(nowStr.compareTo(user.getExpireTime()) > 0){
                //登入失败,账号已过期
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("账号已过期");
            }else if("0".equals(user.getLockState())){
                //登入失败,状态被锁定
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("状态被锁定");
            }else if(!user.getAllowIps().contains(request.getRemoteAddr())){
                //登入失败,ip受限
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("ip受限");
            }else{
                //登入成功
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);

                //保存在session中
                session.setAttribute(Contants.SESSION_USER,user);

                if("true".equals(isRemPwd)){
                    Cookie cookieAct = new Cookie("loginAct",loginAct);
                    Cookie cookiePwd = new Cookie("loginPwd",loginPwd);
                    cookieAct.setMaxAge(10 * 24 * 60 * 60);
                    cookiePwd.setMaxAge(10 * 24 * 60 * 60);
                    response.addCookie(cookieAct);
                    response.addCookie(cookiePwd);
                }else{
                    Cookie cookieAct = new Cookie("loginAct","");
                    Cookie cookiePwd = new Cookie("loginPwd","");
                    cookieAct.setMaxAge(0);
                    cookiePwd.setMaxAge(0);
                    response.addCookie(cookieAct);
                    response.addCookie(cookiePwd);
                }
            }
        }

        return returnObject;
    }

    @RequestMapping(value = "/settings/qx/user/logout.do")
    public String logout(HttpServletResponse response,HttpSession session){
        Cookie cookieAct = new Cookie("loginAct","");
        Cookie cookiePwd = new Cookie("loginPwd","");
        cookieAct.setMaxAge(0);
        cookiePwd.setMaxAge(0);
        response.addCookie(cookieAct);
        response.addCookie(cookiePwd);
        //销毁session
        session.invalidate();

        return "redirect:/";
    }
}